﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace DynamicDNS.Enom
{
    public class Command
    {
        protected readonly IGatewayService Service;
        public NameValueCollection Collection { get; protected set; }
        protected XmlDocument XmlDoc;
        public string Code { get; set; }
        public string Text { get; set; }

        public Command(IGatewayService service)
        {
            Service = service;
            Collection = new NameValueCollection();
            Collection.Add("responsetype", "xml");
        }
        protected void Validate()
        {
            if (InnerText("ErrCount") != "0")
            {
                Console.WriteLine(InnerText("errors/Err1"));
                throw new CommandException(InnerText("errors/Err1"));
            }
        }
        protected string InnerText(string node)
        {
            return XmlDoc.SelectSingleNode("/interface-response/" + node).InnerText;
        }

        protected string GetVariableName<T>(Expression<Func<T>> expr)
        {
            var body = (MemberExpression)expr.Body;
            return body.Member.Name;
        }
    }
}
